package com.x.erp.manage;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.x.erp.dao.MarketDao;
import com.x.erp.dao.UserDao;
import com.x.erp.model.Market;
import com.x.erp.util.ConstantTopic;
import com.x.framework.exception.BusinessException;
import com.x.framework.exception.ExceptionCode;
import com.x.rocketmq.RocketMqProducer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.x.erp.model.Shop;
import com.x.erp.model.User;
import com.x.erp.util.Constant;
import com.x.framework.manage.BaseManage;
import com.x.security.model.Account;
import com.x.security.rpc.SecurityRpcService;

@Service
public class ShopManage extends BaseManage {

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Reference
	private SecurityRpcService securityRpcService;

	@Autowired
	private UserDao userDao;

	@Autowired
	private MarketDao marketDao;

	@Autowired
	private RocketMqProducer rocketMqProducer;

	/**
	 * 插入商铺用户信息
	 * @param user 用户信息
	 * @return
	 * @throws Exception
	 */
	public User insertShopUser(User user) throws Exception {
		Date cDate = this.getDate(0);//当前时间
		Shop shop = user.getShop();
		shop.setShopManageName(user.getUserName());
		shop.setShopManageMobile(user.getUserMobile());
		shop.setShopCreateTime(cDate);
		shop.setShopUpdateTime(cDate);
		shop = this.insert(shop);

		user.setShopId(shop.getShopId());
		user.setUserCreateTime(cDate);
		user.setUserUpdateTime(cDate);
		// 创建用户编号
		createUserNo(user);
		user = this.insert(user);
		String userId = user.getUserId();

		Map<String, String> accountPropertyMap = new HashMap<String, String>();
		accountPropertyMap.put("userId", userId);
		accountPropertyMap.put("marketId", user.getMarketId());
		accountPropertyMap.put("shopId", shop.getShopId());
		Account account = new Account();
		account.setAccountName(user.getUserMobile());
		account.setAccountPassword(user.getUserMobile().substring(5));
		account.setAccountRemark(user.getUserRemark() + user.getUserName());
		account.setAccountState(0);
		account.setAccountCreateTime(cDate);
		account.setAccountModifyTime(cDate);
		account.setAccountPropertyMap(accountPropertyMap);
		account = securityRpcService.insertAccount(account, user.getUserType() + Constant.blank);
		String accountId = account.getAccountId();
		User userBak = new User();
		userBak.setUserId(userId);
		userBak.setAccountId(accountId);
		this.update(userBak);
		try {
			rocketMqProducer.produceMessage(ConstantTopic.TOPIC_ERP_CAR, ConstantTopic.TAG_INSERT_SHOP, user);
		} catch (Exception e) {
			logger.error("新增车商信息插入rocketMq队列异常", e);
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0001, e);
		}
		return user;
	}

	// 生成用户编号
	private void createUserNo(User user) throws Exception {
		// 获取市场编号
		Market marketTemp = new Market();
		marketTemp.setMarketId(user.getMarketId());
		List<Market> list =  marketDao.selectMarketList(marketTemp);
		if(list == null || list.size() == 0){
			throw new BusinessException ("创建用户时，市场Id不正确！");
		}
		marketTemp = list.get(0);
		String marketCode = marketTemp.getMarketCode();

		// 获取当前市场用户的最大用户编号
		Map<String, String> temp = userDao.selectMaxUserNo(user);
		String maxUserNo = temp.get("userNo");
		// 将查询的用户编号获取后4位，数字加1
		Integer userNum = Integer.valueOf(maxUserNo.substring(maxUserNo.length() - 4)) + 1;
		// 将用户编号格式化成4位字符串
		String userCode = String.format("%04d", userNum);
		// 拼接用户编号
		String userNo = marketCode + userCode;
		// 为用户赋值员工编号
		user.setUserNo(userNo);
	}

	public void updateShopUser(User user) throws Exception {
		Date cDate = this.getDate(0);
		Shop shop = user.getShop();
		shop.setShopManageName(user.getUserName());
		shop.setShopManageMobile(user.getUserMobile());
		shop.setShopUpdateTime(cDate);
		user.setUserUpdateTime(cDate);
		this.update(shop);
		this.update(user);
		Map<String, String> accountPropertyMap = new HashMap<String, String>();
		accountPropertyMap.put("userId", user.getUserId());
		accountPropertyMap.put("marketId", user.getMarketId());
		accountPropertyMap.put("shopId", shop.getShopId());
		Account account = new Account();
		account.setAccountId(user.getAccountId());
		account.setAccountName(user.getUserMobile());
		account.setAccountRemark(user.getUserRemark() + user.getUserName());
		account.setAccountModifyTime(cDate);
		account.setAccountPropertyMap(accountPropertyMap);
		securityRpcService.updateAccount(account);
	}
}
